<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="650px"
    @close="close"
  >
    <el-form
      ref="form"
      :inline="true"
      label-width="80px"
      :model="form"
      :rules="rules"
    >
      <el-form-item label="分组" prop="group_id">
        <el-select v-model="form.group_id" placeholder="请选择">
          <el-option
            v-for="item in group"
            :key="item.id"
            :label="item.group_name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="小区" prop="community_id">
        <set-community
          :id.sync="form.community_id"
          @get-data="communityUpdate"
        />
      </el-form-item>
      <el-form-item label="新增套数" prop="number">
        <el-select v-model="form.number" placeholder="请选择" @change="setRoom">
          <el-option
            v-for="item in 99"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="房间数" prop="length">
        <el-select v-model="form.length" placeholder="请选择" @change="setDoor">
          <el-option
            v-for="item in 20"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <div class="roomList">
        <div v-for="(itm, idx) in form.rooms" :key="idx" class="roomBox">
          <div
            v-if="form.rooms.length > 1"
            class="delete"
            @click="deleteFloor(idx)"
          >
            <vab-icon icon="delete-bin-2-line" />
          </div>
          <el-form-item label="门牌号">
            <el-input
              v-model.trim="itm.home_name"
              placeholder="请输入门牌号，例如1号楼1单元101"
            />
          </el-form-item>
          <el-form-item class="doorOut" label="房间号">
            <div class="doorList">
              <div
                v-for="(item1, index1) in itm.rooms_list"
                :key="index1"
                class="door"
              >
                {{ item1 }}
              </div>
            </div>
          </el-form-item>
        </div>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getgroup } from '@/api/group'
import { addRoom } from '@/api/room'
import setCommunity from '@/components/setCommunity'
export default {
  name: 'TogetherBuild',
  components: { setCommunity },
  data() {
    return {
      form: {
        home_type: '2',
        group_id: '',
        community_id: '',
        number: 1,
        length: 1,
        rooms: [{ home_name: '', rooms_list: ['A'] }],
        rooms_data: '',
      },
      word: [
        'A',
        'B',
        'C',
        'D',
        'E',
        'F',
        'G',
        'H',
        'I',
        'J',
        'K',
        'L',
        'M',
        'N',
        'O',
        'P',
        'Q',
        'R',
        'S',
        'T',
        'U',
        'V',
        'W',
        'X',
        'Y',
        'Z',
      ],
      rules: {
        group_id: [{ required: true, trigger: 'blur', message: '请选择分组' }],
        community_id: [
          { required: true, trigger: 'blur', message: '请选择小区' },
        ],
      },
      title: '添加整套',
      dialogFormVisible: false,
      group: [],
    }
  },
  methods: {
    showEdit() {
      this.dialogFormVisible = true
      this.getGroup()
    },
    close() {
      this.$refs['form'].resetFields()
      this.form = this.$options.data().form
      this.dialogFormVisible = false
    },
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          var roomData = this.form.rooms
          var roomArr = []
          for (let i = 0; i < roomData.length; i++) {
            roomArr.push(roomData[i].home_name)
          }
          if (roomArr.includes('')) {
            this.$baseMessage('请输入门牌号', 'error', 'vab-hey-message-error')
          } else if (new Set(roomArr).size !== roomArr.length) {
            this.$baseMessage(
              '门牌号不能重复',
              'error',
              'vab-hey-message-error'
            )
          } else {
            this.form.rooms_data = JSON.stringify(roomData)
            this.goAddRoom(this.form)
          }
        }
      })
    },
    communityUpdate(id) {
      this.form.community_id = id
    },
    async goAddRoom(form) {
      const { data } = await addRoom(form)
      if (data.is_success) {
        this.$baseMessage('添加成功', 'success', 'vab-hey-message-success')
        this.$emit('fetch-data')
        this.close()
      }
    },
    async getGroup() {
      const { data } = await getgroup({})
      this.group = data
    },
    setRoom(num) {
      var date = this.form.rooms
      var lengths = this.form.rooms.length
      var newNum = num - lengths
      var base = {
        home_name: '',
        rooms_list: this.word.slice(0, this.form.length),
      }
      if (lengths > num) {
        // 减少
        this.form.rooms = date.slice(0, newNum)
      } else {
        // 增加
        for (let i = lengths; i < this.form.number; i++) {
          date.push(base)
        }
        this.form.rooms = date
      }
    },
    setDoor(num) {
      var date = this.form.rooms
      var lengths = this.form.rooms.length
      for (let i = 0; i < lengths; i++) {
        date[i].rooms_list = this.word.slice(0, num)
      }
      this.form.rooms = date
    },
    deleteFloor(num) {
      this.form.rooms.splice(num, 1)
    },
  },
}
</script>
<style lang="scss" scoped>
.el-input--small {
  width: 460px;
}
.roomList {
  max-height: 300px;
  overflow: scroll;
  .roomBox {
    padding: 10px;
    background: #f0f7ff;
    border-radius: 10px;
    margin-bottom: 10px;
    position: relative;
    ::v-deep .el-form-item {
      margin-bottom: 0;
    }
    .delete {
      position: absolute;
      top: 10px;
      right: 10px;
      color: #ee5959;
      cursor: pointer;
      width: 32px;
      height: 32px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .rooms {
      width: 100%;
      height: 32px;
      line-height: 32px;
      padding: 0 10px;
      border-radius: 2px;
      color: rgba(0, 0, 0, 0.25);
      background: #f5f5f5;
      border: 1px solid #d9d9d9;
      cursor: not-allowed;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .doorOut {
      margin-top: 10px;
      display: flex;
      ::v-deep .el-form-item__label {
        width: 80px;
      }
      ::v-deep .el-form-item__content {
        width: 440px;
      }
    }
    .doorList {
      display: flex;
      flex-wrap: wrap;
      .door {
        width: 60px;
        height: 32px;
        line-height: 32px;
        padding: 0 10px;
        border-radius: 2px;
        color: rgba(0, 0, 0, 0.25);
        background: #f5f5f5;
        border: 1px solid #d9d9d9;
        cursor: not-allowed;
        white-space: nowrap;
        margin: 0 10px 10px 0;
        text-align: center;
      }
    }
  }
}
</style>
